package com.jackrain.nea.omslocal.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.impl.util.MsgConvertUtil;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.omslocal.Dao.TaoBaoEXChangeDao;
import com.jackrain.nea.omslocal.service.SaveExchangeMsgService;
import com.jackrain.nea.omslocal.service.SendMqServies;
import com.jackrain.nea.util.ApplicationContextHandle;
import lombok.extern.slf4j.Slf4j;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;

import static com.aliyun.openservices.ons.api.Action.CommitMessage;
import static com.aliyun.openservices.ons.api.Action.ReconsumeLater;

/**
 * @Discription: 换货单下载监听
 * Created with IntelliJ IDEA.
 * @Author cs
 * @Date 2018/1/9 16:12
 */
@Slf4j
public class ExchangeOrderListener implements MessageListener {


    @Override
    public Action consume(Message message, ConsumeContext context) {
        SendMqServies sendMq = ApplicationContextHandle.getBean(SendMqServies.class);
        PropertiesConf propertiesConf = ApplicationContextHandle.getBean(PropertiesConf.class);


        try {

            if (propertiesConf.getProperty("ons.exchangeretTopic").equals(message.getTopic())){
                mqExchangeRetConsume(message,context);
            }else if (propertiesConf.getProperty("ons.exchangeTopic").equals(message.getTopic())){
                mqExchangeConsume(message, context);
            }else {
                // 不是监听的topic
                log.error("不是换货单监听的业务");
            }
            return CommitMessage;

        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);

            // 逐条将错误信息以消息发送到线上发送到线上
            JSONObject errObj = new JSONObject();
            errObj.put("topic", message.getTopic());
            errObj.put("tag", message.getTag());
            errObj.put("msgkey", message.getKey());
            // 将错误以流写出
            e.printStackTrace(pw);
            errObj.put("code", -1);
            errObj.put("errmsg", sw.toString());
            errObj.put("protime", System.currentTimeMillis());
            try {
                sendMq.sendMqinfoObj(errObj, propertiesConf.getProperty("ons.loginfor.err.topic"), propertiesConf.getProperty("ons.loginfor.err.tag"));
            } catch (Exception r) {
                log.error("发送日志消息错误" + "========>");
                r.printStackTrace();
            }
            e.printStackTrace();
            return ReconsumeLater;
        }

    }

    /**
     * 换货单下载
     * @param message
     * @param context
     * @throws Exception
     */
    public void mqExchangeConsume(Message message, ConsumeContext context) throws Exception {

        TaoBaoEXChangeDao taoBaoEXChangeDao = ApplicationContextHandle.getBean(TaoBaoEXChangeDao.class);

        String tag = message.getTag();
        Serializable ser = MsgConvertUtil.objectDeserialize(message.getBody());
        JSONObject paramObject = null;

        if (ser instanceof JSONObject) {
            paramObject = JSONArray.parseObject(ser.toString());
        }
        if (paramObject == null) {
            return;
        }
        taoBaoEXChangeDao.exchangeStart(paramObject, message.getKey(),message.getTopic());

    }


    /**
     * 换货功能,卖家确认后货 线上回执
     * @param message
     * @param context
     * @throws Exception
     */
    public void mqExchangeRetConsume(Message message, ConsumeContext context) throws Exception {

        SaveExchangeMsgService saveExchangeMsgService = ApplicationContextHandle.getBean(SaveExchangeMsgService.class);
        JSONArray paramArr = JSONArray.parseArray(MsgConvertUtil.objectDeserialize(message.getBody()).toString());
        String tag = message.getTag();
        if (paramArr == null) {
            throw new NDSException("换货单回执数据为空");

        }
        saveExchangeMsgService.saveExchange(paramArr);

    }

}
